import get from '@dfeidao/atom-web/local/get';
import fire from '@dfeidao/atom-web/msg/fire';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import error from '@dfeidao/atom-web/msgbox/error';
import success from '@dfeidao/atom-web/msgbox/success';
import { IFeidaoAiBrowserComponent } from '@dfeidao/web/interfaces';
import hide_loading from '../../atom/hide-loading';
import show_loading from '../../atom/show-loading';

export default async function a002(fd: IFeidaoAiBrowserComponent) {
	// 保存
	const productid = get(fd, 'productid');
	const f = fd.data.node.querySelector<HTMLFormElement>('#allot_conditions_form1');
	const _id = get(fd, 'x_id');
	const connector = f.connector.value;
	const operate = f.operate.value;
	const field_value = f.field_value.value;
	// console.log(f.operate.selectedOptions[0])
	// const select = fd.data.node.querySelector<HTMLSelectElement>('[name="field"]');
	// const index = select.options.selectedIndex;

	// debugger;

	const selectedOptions = f.field.selectedOptions[0].getAttribute('v');
	const obj = JSON.parse(selectedOptions);
	const field = obj.field;
	const fieldtype = obj.fieldtype;
	const group_id = get(fd, 'group_id');
	const role_allot_id = get(fd, 'role_allot_id');
	const parent_group_id = get(fd, 'parent_group_id');
	if (!field) {
		error('字段名称不能为空');
		return;
	}
	if (!field_value) {
		error('字段值不能为空');
		return;
	}

	const zj = get(fd, 'zj');

	show_loading();
	const res = await nodejs('roler/zj-000008/s002', { _id, connector, operate, field, field_value, fieldtype, group_id, productid });
	hide_loading();
	if (res === 'success') {
		// debugger;
		if (parent_group_id) {
			if (zj === 'zj-000011') {
				fire('zj-000011', 'ec-004', parent_group_id, role_allot_id, _id);

			}
			if (zj === 'zj-000004') {
				fire('zj-000004', 'ec-004', parent_group_id, role_allot_id, _id);
			}
		} else {
			if (zj === 'zj-000011') {
				fire('zj-000011', 'ec-004', group_id, role_allot_id, _id);

			}
			if (zj === 'zj-000004') {
				fire('zj-000004', 'ec-004', group_id, role_allot_id, _id);
			}
			// fire('zj-000004', 'ec-004', group_id, role_allot_id, _id);
		}
		fire('zj-000008', 'eu-001');
		success('修改成功');
	} else {
		error('修改失败');
	}
}
